﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AukcjeInternetowe.Models;
using AukcjeInternetowe.Providers;

namespace AukcjeInternetowe.Controllers
{
    public class AuctionController : Controller
    {
        //
        // GET: /AuctionManager/

        public ActionResult Index()
        {
            if ((Session["User"] as LoginData) == null)
                return View("NotLoggedIn");

            OracleDataProvider db = new OracleDataProvider();
            int id = (Session["User"] as LoginData).UserId;
            return View(db.GetOwnAuctions(id));
        }

        //
        // GET: /AuctionManager/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /AuctionManager/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /AuctionManager/Create

        [HttpPost]
        public ActionResult Create(FormCollection form)
        {
            try
            {
                OracleDataProvider db = new OracleDataProvider();
                db.CreateAuction(new NewAuctionModel()
                {
                    CategoryId = Convert.ToInt32(form["CategoryId"]),
                    UserId = (Session["User"] as LoginData).UserId,
                    Description = Convert.ToString(form["Description"]),
                    EndDate = Convert.ToDateTime(form["EndDate"]),
                    imageUrl = Convert.ToString(form["ImageURL"]),
                    StartingPrice = float.Parse(form["ActualPrice"].Replace('.', ',')),
                    Title = Convert.ToString(form["Title"])
                });

                return RedirectToAction("Index");
            }
            catch (Exception /*exc*/)
            {
                return View();
            }
        }
        
        //
        // GET: /AuctionManager/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /AuctionManager/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AuctionManager/Delete/5
 
        public ActionResult Delete(int id)
        {
            ItemCategoryModel auction = new OracleDataProvider().GetAuction(id);

            if ((Session["User"] as LoginData).UserId != auction.UserId)
                return View("Error", new System.Web.Mvc.HandleErrorInfo(
                    new Exception("Incorrect session. This activity has been logged, and authorities have been informed."),
                    "AuctionController",
                    "Delete"));

            return View(auction);
        }

        //
        // POST: /AuctionManager/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            ItemCategoryModel auction = new OracleDataProvider().GetAuction(id);
            if ((Session["User"] as LoginData).UserId != auction.UserId)
                return View("Error", new System.Web.Mvc.HandleErrorInfo(
                    new Exception("Incorrect session. This activity has been logged, and authorities have been informed."),
                    "AuctionController",
                    "Delete"));

            try
            {
                OracleDataProvider db = new OracleDataProvider();
                db.DeleteAuction(id);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
